package com.sky.controller.admin.Adimin;

import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.SetMealService;
import com.sky.vo.SetmealVO;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/admin/setmeal")
@Slf4j
public class SetMealController {
    @Autowired
    private SetMealService setMealService;
    /**
     * 分页查询
     * */
    @GetMapping("/page")
    @ApiOperation("分页查询")
    public Result page(SetmealPageQueryDTO setmealPageQueryDTO){
        log.info("分页查询{}",setmealPageQueryDTO);
        PageResult setMealPageResult = setMealService.pageSplit(setmealPageQueryDTO);
        return Result.success(setMealPageResult);
    }

    /**
     * 新增套餐
     * */
    @PostMapping
    @ApiOperation("新增套餐")
    @Cacheable(cacheNames = "setmealList",key ="#setmealDTO.categoryId")//与缓存中的名字和id一致
    public Result save(@RequestBody SetmealDTO setmealDTO){
        log.info("新增套餐{}",setmealDTO);
        setMealService.save(setmealDTO);
        return Result.success();
    }

    /**
     * 根据id查询
     * */
    @GetMapping("/{id}")
    public Result getById(@PathVariable Long id){
        log.info("根据id查询{}",id);
        SetmealVO setmealVO = setMealService.getById(id);
        return Result.success(setmealVO);
    }
    /**
     * 起售与停售
     * */
    @PostMapping("/status/{status}")
    @CacheEvict(cacheNames = "setmealDel",allEntries = true)
    public Result startOrStop(@PathVariable Integer status,Long id){
        log.info("设置状态{}id位置{}",status, id);
        setMealService.startOrStop(status,id);
        return Result.success();
    }
    /**
     * 修改setmeal与setmeal_dish
     * */
    @PutMapping
    @CacheEvict(cacheNames = "setmealDel",allEntries = true)
    public Result update(@RequestBody SetmealDTO setmealDTO){
        log.info("修改的信息为{}",setmealDTO);
        setMealService.update(setmealDTO);
        return Result.success();
    }
    /**
     *  删除套餐
     * */
    @DeleteMapping
    @CacheEvict(cacheNames = "setmealDel",allEntries = true)
    public Result deleteGroup(List<Long> ids){
        log.info("要删除的序号有{}",ids);
        setMealService.deleteGroup(ids);
        return Result.success();
    }
}
